<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '300px',
    },
  },
  data() {
    return {
      chart: null,
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)',
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: [
            'car',
            'tricycles',
            'van',
            'box_truck',
            'bus',
            'truck',
            'trailer',
            'traffic_cone',
            'road_barrier',
            'person',
            'bicycle',
            'special_vehiles',
            'animal',
            'unknown_vehicles',
            'unknown_fragment',
            'others',
          ],
        },
        // color:['red', 'green','yellow','blueviolet'],
        series: [
          {
            name: 'WEEKLY WRITE ARTICLES',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: [
              { value: 20, name: 'car', itemStyle: { color: 'rgb(248, 73, 73)' } },
              { value: 20, name: 'van1', itemStyle: { color: 'rgb(87, 160, 7)' } },
              { value: 20, name: 'van2', itemStyle: { color: 'rgb(252, 204, 126)' } },
              { value: 20, name: 'bus', itemStyle: { color: 'rgb(248, 231, 28)' } },
              { value: 20, name: 'truck', itemStyle: { color: 'rgb(174, 245, 95)' } },
              { value: 20, name: 'motorbike', itemStyle: { color: 'rgb(146, 19, 255)' } },
              { value: 20, name: 'tricycle', itemStyle: { color: 'rgb(162, 4, 24)' } },
              { value: 20, name: 'trailer', itemStyle: { color: 'rgb(119, 69, 28)' } },
              { value: 20, name: 'emergencyvehicle', itemStyle: { color: 'rgb(241, 167, 174)' } },
              { value: 20, name: 'trafficcone', itemStyle: { color: 'rgb(223, 108, 246)' } },
              { value: 20, name: 'pedestrian', itemStyle: { color: 'rgb(118, 248, 217)' } },
              { value: 20, name: 'animal', itemStyle: { color: 'rgb(109, 108, 107)' } },
              { value: 20, name: 'other', itemStyle: { color: 'rgb(172, 131, 95)' } },
              { value: 20, name: 'barrier', itemStyle: { color: 'rgb(233, 144, 0)' } },
              { value: 20, name: 'unknown_vehicle', itemStyle: { color: 'rgb(41, 93, 155)' } },
              { value: 20, name: 'road_debris', itemStyle: { color: 'rgb(40, 73, 2)' } },
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600,
          },
        ],
      })
    },
  },
}
</script>
